package com.github.lee.unsplash.ui.auth

import android.content.SharedPreferences
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.MutableLiveData
import com.github.lee.mvvm.core.ui.web.BaseWebViewVM
import com.github.lee.unsplash.BuildConfig
import com.github.lee.unsplash.manager.LoginManager
import com.github.lee.unsplash.model.repository.AuthRepository

class WebAuthVM @ViewModelInject constructor(
    private val authRep: AuthRepository,
    private val lm: LoginManager
) :
    BaseWebViewVM() {

    fun getAuthToken(code: String) {
        requestForResult(
            {
                val clientId = BuildConfig.API_KEY
                val secret = BuildConfig.API_SECRET
                authRep.oauth(clientId, secret, code)

            },
            {
                if (!it.isSuccess()) {
                    postToast("${it.code}:${it.msg}")
                    return@requestForResult
                }
                lm.login(it.data)
                LoginManager.loginSuccessLiveData.postValue(true)

            })
    }

}